package demo4.mapper;

import demo4.tools.IDBUtil;
import demo4.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;

/*
    针对User表进行的增删改插
 */
@Component
public class UserMapper implements IUserMapper{
    @Autowired
    private IDBUtil dbUtil;
    @Override
    public int insert(User user) {
        System.out.println("UserMapper开始插入数据");
        try (Connection connection =dbUtil.getConnection();){
            String sql = "INSERT INTO user(username,password,nickname,age) " +
                         "VALUES (?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3,user.getNickname());
            ps.setInt(4,user.getAge());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
